
clear
set more off

cd "D:\data_replication"


// Construct s20 and s80 for all aggregation levels
//==============================================================================

use estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc8.dta, clear
tostring pc8, replace
gen ll = length(pc8)
gen pc7 = pc8
replace pc7 = substr(pc8, 1, 7) if ll == 8
sort year quarter declarant pc7
by year quarter declarant pc7: egen s_20_pc7 = sum(s_20)
by year quarter declarant pc7: egen s_80_pc7 = sum(s_80)
by year quarter declarant pc7: keep if _n == 1
drop if ll != 8
keep year quarter declarant pc7 s_20_pc7 s_80_pc7
save estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc7.dta, replace


use estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc8.dta, clear
tostring pc8, replace
gen ll = length(pc8)
gen pc6 = pc8
replace pc6 = substr(pc8, 1, 6) if ll == 8
sort year quarter declarant pc6
by year quarter declarant pc6: egen s_20_pc6 = sum(s_20)
by year quarter declarant pc6: egen s_80_pc6 = sum(s_80)
by year quarter declarant pc6: keep if _n == 1
drop if ll != 8
keep year quarter declarant pc6 s_20_pc6 s_80_pc6
save estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc6.dta, replace


use estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc8.dta, clear
tostring pc8, replace
gen ll = length(pc8)
gen pc5 = pc8
replace pc5 = substr(pc8, 1, 5) if ll == 8
sort year quarter declarant pc5
by year quarter declarant pc5: egen s_20_pc5 = sum(s_20)
by year quarter declarant pc5: egen s_80_pc5 = sum(s_80)
by year quarter declarant pc5: keep if _n == 1
drop if ll != 8
keep year quarter declarant pc5 s_20_pc5 s_80_pc5
save estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc5.dta, replace


use estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc8.dta, clear
tostring pc8, replace
gen ll = length(pc8)
gen pc4 = pc8
replace pc4 = substr(pc8, 1, 4) if ll == 8
sort year quarter declarant pc4
by year quarter declarant pc4: egen s_20_pc4 = sum(s_20)
by year quarter declarant pc4: egen s_80_pc4 = sum(s_80)
by year quarter declarant pc4: keep if _n == 1
drop if ll != 8
keep year quarter declarant pc4 s_20_pc4 s_80_pc4
save estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc4.dta, replace


use estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc8.dta, clear
tostring pc8, replace
gen ll = length(pc8)
gen pc3 = pc8
replace pc3 = substr(pc8, 1, 3) if ll == 8
sort year quarter declarant pc3
by year quarter declarant pc3: egen s_20_pc3 = sum(s_20)
by year quarter declarant pc3: egen s_80_pc3 = sum(s_80)
by year quarter declarant pc3: keep if _n == 1
drop if ll != 8
keep year quarter declarant pc3 s_20_pc3 s_80_pc3
save estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc3.dta, replace


use estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc8.dta, clear
tostring pc8, replace
gen ll = length(pc8)
gen pc2 = pc8
replace pc2 = substr(pc8, 1, 2) if ll == 8
sort year quarter declarant pc2
by year quarter declarant pc2: egen s_20_pc2 = sum(s_20)
by year quarter declarant pc2: egen s_80_pc2 = sum(s_80)
by year quarter declarant pc2: keep if _n == 1
drop if ll != 8
keep year quarter declarant pc2 s_20_pc2 s_80_pc2
save estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc2.dta, replace


use estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc8.dta, clear
tostring pc8, replace
rename pc8 pc7
rename s_20 s_20_pc7
rename s_80 s_80_pc7
append using estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc7.dta
rename pc7 pc6
rename s_20_pc7 s_20_pc6
rename s_80_pc7 s_80_pc6
append using estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc6.dta
rename pc6 pc5
rename s_20_pc6 s_20_pc5
rename s_80_pc6 s_80_pc5
append using estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc5.dta
rename pc5 pc4
rename s_20_pc5 s_20_pc4
rename s_80_pc5 s_80_pc4
append using estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc4.dta
rename pc4 pc3
rename s_20_pc4 s_20_pc3
rename s_80_pc4 s_80_pc3
append using estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc3.dta
rename pc3 pc2
rename s_20_pc3 s_20_pc2
rename s_80_pc3 s_80_pc2
append using estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc2.dta
rename pc2 pc
rename s_20_pc2 s_20_pc
rename s_80_pc2 s_80_pc
save estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc.dta, replace


// Compute s20 and s80 for markets and product ids
//==============================================================================

// 2003
//------------------------------------------------------------------------------

forval yy = 2003/2003 {
forval qq = 1/4 {
foreach dd in 1 3 4 5 6 7 8 9 10 11 17 18 30 32 38 {

use estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc.dta, clear
keep if year == `yy' & quarter == `qq' & declarant == `dd'
save estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc_temp.dta, replace

use estimation\2_product_list\output\product_id_all.dta, clear
merge m:1 pc using estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc_temp.dta
sort product_id pc8plus
drop if _merge == 2
replace s_20_pc = 0 if s_20_pc == .
replace s_80_pc = 0 if s_80_pc == .

drop _merge
rename s_20_pc s_20_pc_main
rename s_80_pc s_80_pc_main
rename pc pc_main
rename pc8plus pc
merge m:1 pc using estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc_temp.dta
drop if _merge == 2
sort product_id pc
replace s_20_pc = 0 if s_20_pc == . & _n > 3817
replace s_80_pc = 0 if s_80_pc == . & _n > 3817


sort product_id
by product_id: egen s_20_pc_sub = sum(s_20_pc)
by product_id: egen s_80_pc_sub = sum(s_80_pc)
by product_id: keep if _n == 1

gen s_20_product_id = s_20_pc_main + s_20_pc_sub
gen s_80_product_id = s_80_pc_main + s_80_pc_sub
keep year quarter declarant product_id s_20_product_id s_80_product_id

replace year = `yy' if year == .
replace quarter = `qq' if quarter == .
replace declarant = `dd' if declarant == .

save estimation\4_demand_estimation\4_cobb_douglas_weights\weights_product_id_`yy'_`qq'_`dd'.dta, replace


rm estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc_temp.dta

}
}
}


// 2004-2005
//------------------------------------------------------------------------------

forval yy = 2004/2005 {
forval qq = 1/4 {
foreach dd in 1 3 4 5 6 7 8 9 10 11 17 18 30 32 38 46 53 54 55 60 61 63 64 66 68 91 600 {

use estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc.dta, clear
keep if year == `yy' & quarter == `qq' & declarant == `dd'
save estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc_temp.dta, replace

use estimation\2_product_list\output\product_id_all.dta, clear
merge m:1 pc using estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc_temp.dta
sort product_id pc8plus
drop if _merge == 2
replace s_20_pc = 0 if s_20_pc == .
replace s_80_pc = 0 if s_80_pc == .

drop _merge
rename s_20_pc s_20_pc_main
rename s_80_pc s_80_pc_main
rename pc pc_main
rename pc8plus pc
merge m:1 pc using estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc_temp.dta
drop if _merge == 2
sort product_id pc
replace s_20_pc = 0 if s_20_pc == . & _n > 3817
replace s_80_pc = 0 if s_80_pc == . & _n > 3817


sort product_id
by product_id: egen s_20_pc_sub = sum(s_20_pc)
by product_id: egen s_80_pc_sub = sum(s_80_pc)
by product_id: keep if _n == 1

gen s_20_product_id = s_20_pc_main + s_20_pc_sub
gen s_80_product_id = s_80_pc_main + s_80_pc_sub
keep year quarter declarant product_id s_20_product_id s_80_product_id

replace year = `yy' if year == .
replace quarter = `qq' if quarter == .
replace declarant = `dd' if declarant == .

save estimation\4_demand_estimation\4_cobb_douglas_weights\weights_product_id_`yy'_`qq'_`dd'.dta, replace


rm estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc_temp.dta

}
}
}


rm estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc2.dta
rm estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc3.dta
rm estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc4.dta
rm estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc5.dta
rm estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc6.dta
rm estimation\4_demand_estimation\4_cobb_douglas_weights\weights_pc7.dta


// Collect results and save
//==============================================================================

use estimation\4_demand_estimation\4_cobb_douglas_weights\weights_product_id_2003_1_1.dta, clear
gen temp = 1

forval yy = 2003/2003 {
forval qq = 1/4 {
foreach dd in 1 3 4 5 6 7 8 9 10 11 17 18 30 32 38 {

append using estimation\4_demand_estimation\4_cobb_douglas_weights\weights_product_id_`yy'_`qq'_`dd'.dta
rm estimation\4_demand_estimation\4_cobb_douglas_weights\weights_product_id_`yy'_`qq'_`dd'.dta

}
}
}


forval yy = 2004/2005 {
forval qq = 1/4 {
foreach dd in 1 3 4 5 6 7 8 9 10 11 17 18 30 32 38 46 53 54 55 60 61 63 64 66 68 91 600 {

append using estimation\4_demand_estimation\4_cobb_douglas_weights\weights_product_id_`yy'_`qq'_`dd'.dta
rm estimation\4_demand_estimation\4_cobb_douglas_weights\weights_product_id_`yy'_`qq'_`dd'.dta

}
}
}

drop if temp == 1
drop temp
save estimation\4_demand_estimation\4_cobb_douglas_weights\weights_product_id.dta, replace


